#include <sys/system.h>
#include <sys/string.h>
#include <widgets/object.h>

static pt_list_t _ptgui_type_top_list;

static void _ptgui_object_constructor(ptgui_object_t *object)
{
	if (!object) {
		return;
	}

	object->is_static = PT_FALSE;
}

/* Destroys the object */
static void _ptgui_object_destructor(ptgui_object_t *object)
{
	/* nothing */
}

void ptgui_type_system_init(void)
{
	pt_list_init(&_ptgui_type_top_list);
}

void ptgui_type_system_free(void)
{
	struct pt_list_node *node;

	node = _ptgui_type_top_list.next;
	while (node != PT_NULL) {
		ptgui_type_t *child = pt_list_entry(node, ptgui_type_t, sibling);

		pt_list_remove(&_ptgui_type_top_list, &(child->sibling));

		child->parent = PT_NULL;

		/* destroy object and remove from parent */
		ptgui_type_destroy(child);

		node =  _ptgui_type_top_list.next;
	}
}

ptgui_type_t * ptgui_type_create(const pt_char_t *type_name, ptgui_type_t *parent_type, pt_int16_t type_size,
								 ptgui_constructor_t constructor, ptgui_destructor_t destructor)
{
	ptgui_type_t *new_type;

	if (!type_name) {
		return PT_NULL;
	}

	new_type = pt_malloc(sizeof(ptgui_type_t));
	new_type->name = pt_strdup(type_name);
	new_type->size = type_size;
	new_type->constructor = constructor;
	new_type->destructor = destructor;
	pt_list_init(&new_type->sibling);
	pt_list_init(&new_type->child);

	if (!parent_type) {
		new_type->parent = PT_NULL;
		pt_list_append(&_ptgui_type_top_list, &new_type->sibling);
	}
	else {
		new_type->parent = parent_type;
		pt_list_append(&parent_type->child, &new_type->sibling);
	}


	return new_type;
}

void ptgui_type_destroy(ptgui_type_t *type)
{
	struct pt_list_node *node;

	if (!type) {
		return;
	}

	node = type->child.next;
	while (node != PT_NULL) {
		ptgui_type_t *child = pt_list_entry(node, ptgui_type_t, sibling);

		pt_list_remove(&(type->child), &(child->sibling));

		child->parent = PT_NULL;

		/* destroy object and remove from parent */
		ptgui_type_destroy(child);

		node = type->child.next;
	}

	pt_free(type->name);
	pt_free(type);
}

void ptgui_type_object_construct(ptgui_type_t *type, ptgui_object_t *object)
{
	if (!type || !object) {
		return;
	}

	/* Call parent constructor */
	if (type->parent != PT_NULL) {
		ptgui_type_object_construct(type->parent, object);
	}

	if (type->constructor) {
		type->constructor(object);
	}
}

void ptgui_type_destructors_call(ptgui_type_t *type, ptgui_object_t *object)
{
	if (!type || !object) {
		return;
	}

	if (type->destructor) {
		type->destructor(object);
	}

	while (type->parent != PT_NULL) {
		type = type->parent;
		if (type->destructor) {
			type->destructor(object);
		}
	}
}

pt_bool_t ptgui_type_inherits_from(ptgui_type_t *type, ptgui_type_t *parent)
{
	if (!type || !parent) {
		return PT_FALSE;
	}

	while (type == parent) {
		return PT_TRUE;
	}

	while (type->parent != PT_NULL) {
		type = type->parent;
		if (type == PT_NULL) {
			break;
		}
		
		if (type == parent) {
			return PT_TRUE;
		}
	}

	return PT_FALSE;
}

ptgui_type_t * ptgui_type_parent_type_get(ptgui_type_t *type)
{
	if (!type || !type->parent) {
		return PT_NULL;
	}

	return type->parent;
}

const pt_char_t * ptgui_type_name_get(ptgui_type_t *type)
{
	if (!type) {
		return PT_NULL;
	}

	return type->name;
}

struct ptgui_object_information
{
	pt_uint32_t	objs_number;
	pt_uint32_t	allocated_size;
	pt_uint32_t	max_allocated;
};
struct ptgui_object_information obj_info =
{
	0,
	0,
	0
};

/**
 * @brief Creates a new object: it calls the corresponding constructors (from the constructor of the base class to the
 * constructor of the more derived class) and then sets the values of the given properties
 * @param object_type the type of object to create
 * @return Returns the new Etk_Object of type @a object_type
 */
ptgui_object_t * ptgui_object_create(ptgui_type_t *object_type)
{
	ptgui_object_t *new_object;

	if (!object_type) {
		return PT_NULL;
	}

	new_object = pt_malloc(object_type->size);
	if (new_object == PT_NULL) {
		return PT_NULL;
	}

	obj_info.objs_number ++;
	obj_info.allocated_size += object_type->size;
	if (obj_info.allocated_size > obj_info.max_allocated) {
		obj_info.max_allocated = obj_info.allocated_size;
	}

	new_object->type = object_type;
	new_object->is_static = PT_FALSE;

	ptgui_type_object_construct(object_type, new_object);

	return new_object;
}

/**
 * @brief Destroys the object: it first sets the weak-pointers to PT_NULL, emits the "destroyed" signal, and then
 * queues the object in the list of objects to free. Thus, the destructors will only be called at the beginning of the
 * next main loop iteration (from the destructor of the more derived class to the destructor of the ultimate base class).
 * @param object the object to destroy
 * @warning You should not assume that this function will call directly the destructors of the object!
 */
void ptgui_object_destroy(ptgui_object_t *object)
{
	if (!object || object->is_static == PT_TRUE) {
		return;
	}

	obj_info.objs_number --;
	obj_info.allocated_size -= object->type->size;

	/* call destructor */
	PT_ASSERT(object->type != PT_NULL);
	ptgui_type_destructors_call(object->type, object);

	/* release object */
	pt_free(object);
}

/**
 * @internal
 * @brief Gets the type of a ptgui_object
 * @return Returns the type of a ptgui_object
 */
ptgui_type_t * ptgui_object_type_get(void)
{
	static ptgui_type_t *object_type = PT_NULL;

	if (!object_type) {
		object_type = ptgui_type_create("object", PT_NULL, sizeof(ptgui_object_t),
										PTGUI_CONSTRUCTOR(_ptgui_object_constructor),
										PTGUI_DESTRUCTOR(_ptgui_object_destructor));
	}

	return object_type;
}

/**
 * @brief Checks if @a object can be cast to @a type.
 * If @a object doesn't inherit from @a type, a warning is displayed in the console but the object is returned anyway.
 * @param object the object to cast
 * @param type the type to which we cast the object
 * @return Returns the object
 * @note You usually do not need to call this function, use specific macros instead (ETK_IS_WIDGET() for example)
 */
ptgui_object_t * ptgui_object_check_cast(ptgui_object_t *object, ptgui_type_t *type)
{
	if (!object) {
		return PT_NULL;
	}

	if (!ptgui_type_inherits_from(object->type, type)) {
		pt_kprintf("Invalid cast from \"%s\" to \"%s\"\n", ptgui_type_name_get(object->type),
				   ptgui_type_name_get(type));
	}

	return object;
}

/**
 * @brief Gets the type of the object
 * @param object an object
 * @return Returns the type of @a object (PT_NULL on failure)
 */
ptgui_type_t * ptgui_object_object_type_get(ptgui_object_t *object)
{
	if (!object) {
		return PT_NULL;
	}

	return object->type;
}
